<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>state使用和生命周期</title>
</head>
<body>
  <div id="app"></div>
  <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
  <!-- 使用jsx需要babel转义 -->
  <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
  <!-- 使用babel需要申明type -->
  <script type="text/babel">
    const tag = document.getElementById('app')
    // Clock组件
    function Clock2(props) {
      const ele = (
        <div>
          <h1>hello world</h1>
          <h2>现在是:{props.date.toLocaleTimeString()}</h2>
        </div>
      )
      return ele
    }
    // 状态组件
    class Clock extends React.Component{
      constructor(props) {
        super(props)
        this.state= {
          date: new Date(),
          count: 1
        }
        console.log('constructor',props)
      }
      tick() {
        // this.setState({
        //   date: new Date()
        // })
        // 牵扯到计算的最好使用函数更改
        this.setState((prevState,props) => ({
          date: new Date(),
          count: prevState.count + 1
        }))
      }
      // 组件销毁时
      componentWillUnmount() {
        console.log('componentWillUnmount')
        clearInterval(this.timeID)
      }
      // dom渲染完成后
      componentDidMount() {
        this.timeID = setInterval(() => {
          this.tick()
        },1000) 
        console.log('componentDidMount')
      }
      render() {
        let { date, count } = this.state
        console.log('render',count)
        const ele = (
          <div>
            <h1>hello state状态{this.props.title}</h1>
            <h2>现在是:{date.toLocaleTimeString()}</h2>
          </div>
        )
        return ele
      }
    }
    ReactDOM.render(
        <Clock title='组件' />,
        tag
      )
    // function tick() {
    
    // }
    // setInterval(() => {
    //   console.log('tick')
    //   tick()
    // },1000)
  </script>
</body>
</html>